const sequelize = require("../config/sequelize");
const User = require("../models/user");
const Fans = require("../models/fans");
const messageService = require("./message");
const userService = require("./userService");


//判断是否关注
exports.isConcern = async function(noticerId,followerId){
    const fan = await Fans.findOne({
        where:{
            noticerId,followerId
        }
    });
    if(fan){
        return true;
    }else{
        return false;
    }

}

//取消关注
exports.unfollow = async function(noticerId,followerId){
    await Fans.destroy({
        where:{
            noticerId,followerId
        },
        individualHooks:true
    });

    //删除消息
    await messageService.deleteFollow(followerId,noticerId,3);

}

//关注
exports.follow = async function(noticerId,followerId){
    await Fans.create({
        noticerId,followerId
    });

    //添加消息
    await messageService.add('关注了你',followerId,noticerId,3,0,0);
}

//获取关注列表
exports.getFollowList = async function(noticerId){
    let follows = await Fans.findAll({
        where:{noticerId},
    });
    let length = follows.length;
    for(let i=0;i<length;i++){
        follows[i].dataValues.user = await userService.getInfo(follows[i].followerId);
        follows[i].dataValues.isConcern = true;
    }
    console.log('follows:',follows)
    return follows;
}

//获取粉丝列表
exports.getFansList = async function(followerId){
    let fans = await Fans.findAll({
        where:{followerId}
    });
    let length = fans.length;
    for(let i=0;i<length;i++){
        fans[i].dataValues.user = await userService.getInfo(fans[i].noticerId);
        let isConcern = await Fans.findOne({
            where:{
                noticerId:followerId,
                followerId:fans[i].noticerId
            }
        });
        fans[i].dataValues.isConcern = isConcern?true:false;
    }
    return fans;
}


